package com.secret.service;

import com.alibaba.fastjson2.*;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;

public class ConfigService {
    private JSONObject data;

    private File file;

    private ConfigService(String path) {
        this.file = new File(path);
        if(file.exists()){
            this.load();
        }else{
            this.data = new JSONObject();
        }
    }

    private void load(){
        try {
            byte[] bytes = Files.readAllBytes(this.file.toPath());
            String text = new String(bytes , "UTF-8");
            this.data = JSON.parseObject(text);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void save(){
        String json = JSON.toJSONString(data , JSONWriter.Feature.PrettyFormat);
        try {
            Files.write(this.file.toPath() , json.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }





    private static ConfigService instance;

    public static ConfigService getInstance(){
        if(instance == null){
            String home = System.getProperty("user.home");
            Path path = Paths.get(home, "secret-data.json");
            instance = new ConfigService(path.toString());
        }

        return instance;
    }

    /**
     * 设置上一次打开的目录位置
     * @param lastOpenDir
     */
    public void setLastOpenDir(File lastOpenDir) {
        this.data.put("lastOpenDir" , lastOpenDir.getAbsolutePath());
        this.save();
    }

    public String getLastOpenDir(){
        String lastOpenDir = this.data.getString("lastOpenDir");
        return lastOpenDir;
    }

    public void addFile(File file) {
        if(exists(file)){
            return;
        }
        List<String> files = getFiles();
        files.add(file.getAbsolutePath());
        this.data.put("files" , files);
        this.save();
    }

    public boolean exists(File file){
        String name = file.getName();
        List<String> files = getFiles();
        for (String s : files) {
            File f = new File(s);
            String fName = f.getName();
            return name.equals(fName);
        }
        return false;
    }

    public List<String> getFiles(){
        JSONArray files = this.data.getJSONArray("files");
        if(files == null){
            return new ArrayList<>();
        }
        List<String> list = files.toList(String.class);
        return list;
    }

    public String getFileByName(String name) {
        List<String> files = getFiles();
        for (String file : files) {
            File f = new File(file);
            if(f.getName().equals(name)){
                return file;
            }
        }
        return null;
    }

    public void removeFile(String file) {
        List<String> files = getFiles();
        files.remove(file);
        this.data.put("files" , files);
        this.save();
    }
}
